/**
 * Created by chenyu on 17/10/18.
 */
/**
 * Created by chenyu on 17/9/30.
 */
var fs = require('fs');
var env = process.argv[2] || process.env.NODE_ENV || 'development';
var config = require('./config/config')[env];
var MongoClient = require('mongodb').MongoClient;
var DB_CONN_STR = config.db;

var data = fs.readFileSync('test.json').toString();
var insertData = function (db, callback) {

    var areaCollection = db.collection('areas');

    areaCollection.find({}).toArray(function (err, result) {
        if (result.length === 0) {
            areaCollection.insert(JSON.parse(data).zone, function (err, result) {
                if (err) {
                    console.log('Error:' + err);
                    return db.close();
                }
                callback(result);
                console.log("初始化area成功")
            });
        }else {
            console.log("数据存在");
            db.close();
        }
    })
};

MongoClient.connect(DB_CONN_STR, function (err, db) {
    insertData(db, function (result) {
        db.close();
    });
});